<!-- 购物车 -->
<template>
	<view >
		<!-- 总数 -->
		<view class="head_box" v-show="!isEmpty">
			<view class="tool-box u-flex u-row-between">
				<view class="count-box">
					共
					<text class="all-num">{{ cartList.length }}</text>
					件商品
				</view>
				<button class="u-reset-button set-btn" @tap="isTool = !isTool">{{ isTool ? '完成' : '编辑' }}</button>
			</view>
		</view>

		<view class="content_box" :style="cartList.length ? 'padding-bottom: 220rpx;':''">
			<!-- 列表 -->
			<u-checkbox-group @change="checkboxGroupChange" activeColor="#e9b461" v-if="!isEmpty"> 
				<view class="collect-list u-flex u-row-left u-col-center" v-for="(goods, index) in cartList" :key="index">
					<u-checkbox class="u-p-l-10" :name="goods.id" shape="circle" v-model="goods.checked" @change="ongoodsSel(goods)" >
						<view style="height: 160rpx"></view>
					</u-checkbox>
					<view class="goods-wrap">
						<view @click="goStore(goods.storeId)" class="store">
							<img class="store-icon" src="https://image.hzwltb69.com/app/imgs/score/store.png" alt="">
							<view class="con">
								{{goods.storeName}}
							</view>
							<img class="arrow" src="https://image.hzwltb69.com/app/date/2023/rightarr.png" alt="">
						</view>
						<!-- <view class="lose-box"
							v-if="goods.cart_type === 'invalid' || (goods.cart_type === 'activity' && !isActivityPay)">
							<text v-if="goods.cart_type === 'invalid'" class="iconfont icon-yishixiao"></text>
							<view v-if="goods.cart_type === 'activity' && !isActivityPay"
								class="invalid-tips u-flex u-row-center u-col-center">活动商品,仅支持单独购买</view>
						</view> -->
						<ljshop-mini-card :image="goods.skuThumbnail" :title="goods.skuName"
							@click="$Router.push({ path: '/pages/goods/detail', query: { id: goods.goodsId } })">
							<template #describe>
								<view class="order-sku u-ellipsis-1"  >
									<text v-for="(spec, index) in goods.specificationValues" :key="index"
										class="order-num">{{ spec.value ? spec.value : '' }}</text>
								   </view>
							</template>
							<template #cardBottom>
								<view class="order-price-box u-flex u-row-between" @tap.stop >
									<text
										class="order-price font-OPPOSANS">￥{{ goods.subtotal ? goods.subtotal : 0 }}</text>
									<u-number-box :value="goods.quantity"
									 :long-press="false"
									 :min="goods.buyNum.minNum" 
									 :step="goods.buyNum.stepNum"
										:index="index"  @min="onMin(goods)"
										@minus="changeNum($event, goods)"
									    @plus="changeNum($event, goods)"
										@change="changeNum($event, goods)">
									</u-number-box>
								</view>
							</template>
						</ljshop-mini-card>
					</view>
				</view>
			</u-checkbox-group>
			<view
			class="upward"
			v-if="isShow"
			@click="pullTop()">
			  <u-icon
			    name="arrow-upward"
				color="#999"
				size="28">
			   </u-icon>
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="foot_box " v-show="!isEmpty">
			<view class="tools-box u-flex u-row-between">
				<u-checkbox @change="onAllSel" activeColor="#e9b461" shape="circle" :value="allSelected">
					全选（{{ totalCount.totalNum }}）</u-checkbox>
				<view class="u-flex">
					<view class="price font-OPPOSANS" v-show="!isTool">￥{{totalCount.totalPrice.toFixed(2) }}{{totalCount.exchangePoint?"+"+totalCount.exchangePoint+"积分":''}}</view>
					<button class="u-reset-button pay-btn" :disabled="!isSel" v-show="!isTool" @tap="onPay">结算</button>
					<button class="u-reset-button del-btn" v-show="isTool" @tap="goodsDelete">删除</button>
				</view>
			</view>
		</view>
		<!-- <ljshop-tabbar></ljshop-tabbar> -->
		
		<ljshop-empty-cart v-if="isEmpty" :image="'https://image.hzwltb69.com/app/date/2023/暂无商品.png'" tipText="暂无商品">
		</ljshop-empty-cart> 
		
		<view class="showTitle" v-if="isEmpty">
			<image class="slash" src="https://image.hzwltb69.com/app/date/2023/goods/f82eb1d17507265143ed63f78f1a357.png" mode=""></image>
			大家都在买
			<image class="slash" src="https://image.hzwltb69.com/app/date/2023/goods/f82eb1d17507265143ed63f78f1a357.png" mode=""></image>
		</view>
		<!-- 推荐商品 -->
		<!-- <sh-hot-goods-cart v-if="isEmpty" ></sh-hot-goods-cart> -->
		<shHotList v-if="isEmpty" :listType="1" ref="hotlist" :cart="false"></shHotList>
    	<shNavBar style="position: fixed;bottom: 0;left: 0;right: 0;z-index: 999;" class="shNavBar" :select="3" :list="iconList" :list2="iconList2" :count="cartList.length"></shNavBar>
	</view>
</template>

<script>
	import shBanner from './components/sh-banner.vue';
  import shNavBar from './components/sh-nav-bar.vue';
	// import shHotGoodsCart from './components/sh-hot-goods-cart.vue';
	import shTitleCard from './components/sh-title-card.vue';
	import shHotList from './components/sh-hot-goods-usertwo.vue'
 
	let timer = null;
	export default {
		components: {
			// shHotGoodsCart,
			shTitleCard,
			shHotList,
      shNavBar
		},
		data() {
			return {
				isShow: false,
				maxStep: 999,
				isTool: false,
				isEmpty:true,
				cartList: [], //购物车列表
				checkCart: {}, //检测是否是购物车数据的对象
				cartNum: 0, //购物车,涉及到刷新数据丢失，所以存了本地,
				cartData:{},
				totalCount:{
					totalPrice:0,
					totalNum:0
				},
				isSel:false,
				allSelected:false,
				iconList: uni.getStorageSync('homeModule'),
				iconList2: uni.getStorageSync('homeModule2'),
			};
		},
		onPageScroll(e) {
			// this.isScorll = e.scrollTop > 100 ? true : false;
			if (e.scrollTop > 500) {
				this.isShow = true;
			} else {
				this.isShow = false;
			}
		},
		onShow() {
			this.allSelected = false
			  this.getCartList();
			  // this.initCart();
		},
		onHide() {
			this.isTool = false;
		},
		onReachBottom(){
			if(this.isEmpty){
				this.$refs.hotlist.page += 1
				this.$refs.hotlist.hasUserGet()
			}
		},
		methods: {
			pullTop() {
				uni.pageScrollTo({
					scrollTop: 0, //滚动到距离顶部为0
					duration: 500 //滚动时长
				})
			},
			goStore(e){
				this.$Router.push({
					path: "/pages/store/index",
					query:{
						id:e
					}
				})
			},
			// 到达最小值
			onMin(g) {
				uni.showModal({
					title: '删除提示',
					confirmColor: '#f0c785',
					content: `是否确认从购物车中删除此商品?`,
					success: res => {
						res.confirm && this.deleteCartList(g.skuId);
					}
				});
			},
			// 更改商品数
			changeNum(e, g) {
				e.value > 0 && this.changeCartList({ ids: [g.skuId], goodsNum: e.value, art: 'change' });
			},

			// 单选
			checkboxGroupChange(e) {
				this.$store.commit('checkCartList');
			},

			// 路由跳转
			jump(path, parmas) {
				this.$Router.push({
					path: path,
					query: parmas
				});
			},

			// 全选
			onAllSel() {
				let that = this;
				that.allSelected=!that.allSelected;
				that.cartList.map(item => {
					item.checked = that.allSelected
				})
				if(!that.allSelected){
					this.isSel=false
				}else{
					this.isSel=true
				}
				this.totalCount = this.settotalCount();
			},
			ongoodsSel(goods){
				goods.checked = !goods.checked;
				this.checkCartList();
				this.totalCount = this.settotalCount();
				
				let that = this;
				let { cartList } = this;

					let isActivity = false;
					for (let item of this.cartList) {
						if (item.checked) {
							 isActivity =true;
							 break;
						}
				}
				this.isSel=isActivity;
				
			},
			// 全选检测
			checkCartList() {
				let that = this;
				let all = true;
				that.cartList.map(item => {
					if (!item.checked) {
						all = false
					}
					
				})
				that.allSelected = all;
			},

			// 结算
			onPay() {
				let that = this;
				let { cartList } = this;
				if (this.isSel) {
					let confirmcartList = [];
					let isActivity = false;
					for (let item of this.cartList) {
						if (item.checked) {
							if (item.cart_type === 'invalid') {
								this.$u.toast('商品已失效');
								return false;
							}
							if (item.cart_type === 'activity') {
								isActivity = true;
							}
							confirmcartList.push({
								skuId: item.skuId,
								quantity: item.quantity,
							});
						}
					}
					if (confirmcartList.length > 1 && isActivity) {
						this.$u.toast('活动商品只能单独购买');
						return false;
					}
					 
					that.jump('/pages/order/confirm', { goodsList: confirmcartList, from: 'cart' });
				}
			},
			// 删除
			goodsDelete() {
				let that = this;
				let { cartList } = this;
				//console.info(JSON.stringify(this.cartList))
				let selectedIdsArray = [];
				for(let i = 0;i<that.cartList.length;i++){
					if(that.cartList[i].checked){
					//	selectedIdsArray.push(that.cartList[i].skuId)
					that.deleteCartList(that.cartList[i].skuId)
					}
					
				}
				//this.deleteCartList({ ids: selectedIdsArray, art: 'delete' });
			},
			deleteCartList(ids){
				this.$http('cart.remove', {
					skuId: ids,
					isTransfer: 'false',
					//UId: uni.getStorageSync('Uid')
					UId:uni.getStorageSync("UId")
				}).then(res => {
			        this.getCartList();
					// this.initCart();
				})
			},
			getCartList(){
				uni.showLoading({
					
				})
				this.$http('cart.index',{
					UId:uni.getStorageSync("UId")
				}).then(res => {
				 uni.hideLoading()
					if(res.code==1){
						 // this.initCart();
						let cartData = res.data;
						if(cartData.length > 0 ) {
							this.isEmpty = false;
						} else {
							this.isEmpty = true;
						}
						this.isSel =false
						cartData.length && cartData.map(item => {
								item.checked = false;
							})
							this.cartList = cartData;		
						this.totalCount = this.settotalCount();
					} else {
						this.cartList=[];
						this.totalCount={
							totalPrice:0,
							totalNum:0
						}
						this.isEmpty=true;
					}
				}) 
				 
			},
			
			settotalCount(){
				let totalNum = 0;
				let totalPrice = 0;
				let exchangePoint = 0;
				this.cartList.filter(item => {
					if (item.checked) {
						totalNum += 1;
						totalPrice += item.quantity * (item.price ? item.price : 0);
						exchangePoint+= item.quantity * (item.exchangePoint ? item.exchangePoint : 0);
					}
				})
				return {
					totalNum,
					totalPrice
				}
				
			},
			// 添加到购物车（增）
			addCartGoods(){
				this.$http('cart.add', {
					skuId: data.list[0].skuId,
					count: data.list[0].count,
				 	UId: uni.getStorageSync("UId")
				}).then(res => {
					res.code === 1 && this.getCartList()
					resolve(res)
				}).catch(e => {
					reject(e)
				})
			
			},
			changeCartList(skuId,count){
				this.$http('cart.edit', {
						skuId:skuId.ids[0],
						count: skuId.goodsNum || null,
						UId:uni.getStorageSync("UId")
					}).then(res => {
					    // this.initCart();
					    this.getCartList();
					})
			},
			// initCart(){
			// 	let that = this;
			// 	uni.showLoading({})
			// 	 that.$http(
			// 	 	'common.initcart',{
			// 			UId:uni.getStorageSync("UId")
			// 		},
			// 	 ).then(res => {
			// 		uni.setStorageSync("cartCount",res.count
			// 		)
			// 		 uni.hideLoading()
			// 	 });
			// 	 uni.stopPullDownRefresh();
			// 	 this.isRefresh = true;
			// }
		}
	};
</script>

<style lang="scss">
   page{
		height: initial;
		overflow-y: initial;
		min-height: 100%;
	}
	// 总计商品
	.head_box {
		.tool-box {
			height: 90rpx;
			padding: 0 30rpx;
			background: #f7f5f6;

			.count-box {
				font-size: 26rpx;
				color: #999;

				.all-num {
					color: #f9731e;
				}
			}

			.set-btn {
				background: none;
				font-size: 26rpx;
				color: #f9731e;
			}
		}
	}

	// 空白页
	.empty-box {
		flex: 1;
		width: 100%;
		height: 100%;
	}

	// 购物车项
	.collect-list {
		background: #fff;
		width: 750rpx;
		padding: 20rpx 10rpx 20rpx;

		// 商品卡片包裹
		.goods-wrap {
			position: relative;
			.store{
			margin-bottom: 15rpx;
			display: flex;
			align-items: center;
			font-size: 30rpx;
			// font-weight: 600;
			.con{
				margin-left: 10rpx;
				margin-right: 10rpx;
			}
			.store-icon{
				width: 40rpx;
				height: 35rpx;
			}
			.arrow{
				width: 20rpx;
				height: 20rpx;
			}
			}
			.order-sku {
				font-size: 24rpx;
				font-weight: 400;
				color: rgba(153, 153, 153, 1);
				width: 440rpx;
				margin-bottom: 20rpx;

				.order-num {
					margin-right: 10rpx;
				}
			}

			.order-price-box {
				.status-btn {
					height: 32rpx;
					border: 1rpx solid rgba(207, 169, 114, 1);
					border-radius: 15rpx;
					font-size: 20rpx;
					font-weight: 400;
					color: rgba(168, 112, 13, 1);
					padding: 0 10rpx;
					margin-left: 20rpx;
					background: rgba(233, 183, 102, 0.16);
				}

				.order-price {
					font-size: 26rpx;
					font-weight: 600;
					color: #ff0000;
				}
			}

			.lose-box {
				position: absolute;
				z-index: 10;
				width: 100%;
				height: 100%;
				background-color: rgba(#fff, 0.8);

				.icon-yishixiao {
					position: absolute;
					bottom: 0rpx;
					right: 80rpx;
					font-size: 140rpx;
					line-height: 140rpx;
					color: #dbdbdb;
					transform: rotate(-30deg);
				}

				.invalid-tips {
					position: absolute;
					top: 0;
					right: 0;
					left: 0;
					bottom: 0;
					margin: auto;
					width: 400rpx;
					height: 60rpx;
					border-radius: 30rpx;
					color: #fff;
					background-color: rgba(#000, 0.35);
				}
			}
		}

		.tag-box {
			.tag1 {
				line-height: 36rpx;
				padding: 0 8rpx;
				font-size: 18rpx;
				color: rgba(#fff, 0.9);
				background: #f39800;
				display: inline-block;
				box-sizing: border-box;
			}

			.tag2 {
				line-height: 34rpx;
				padding: 0 8rpx;
				font-size: 18rpx;
				color: rgba(#f39800, 0.9);
				background: #fff;
				border-top: 1rpx solid #f39800;
				border-right: 1rpx solid #f39800;
				border-bottom: 1rpx solid #f39800;
				display: inline-block;
				box-sizing: border-box;
			}
		}

		.price-box {
			width: 420rpx;

			.price {
				color: #e1212b;
			}
		}
	}

	.tools-box {
		height: 100rpx;
		width: 750rpx;
		padding: 0 20rpx;
		background: #fff;

		.check-all {
			font-size: 26rpx;

			.check-all-radio {
				transform: scale(0.7);
				color: #e9b564;
			}
		}

		.price {
			color: #ff0000;
			font-size: 500;
			margin-right: 30rpx;
		}

		.pay-btn {
			width: 200rpx;
			line-height: 70rpx;
			//background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
			background: #f9731e;
			box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
			border-radius: 35rpx;
			padding: 0;
			color: rgba(#fff, 0.9);
		}

		.del-btn {
			width: 200rpx;
			line-height: 70rpx;
			background: linear-gradient(90deg, rgba(244, 71, 57, 1) 0%, rgba(255, 102, 0, 1) 100%);
			border-radius: 35rpx;
			padding: 0;
			color: rgba(#fff, 0.9);
		}
	}
	.foot_box{
		position: fixed;
		bottom: 120rpx;
		z-index: 999;
	}
	
	.showTitle{
		width: 100%;
		height: 80rpx;
		font-size: 30rpx;
		font-weight: bold;
		color: #D30715;
		background: #f6f6f6;
		display: flex;
		justify-content: center;
		align-items: center;
		.slash{
			width: 60rpx;
			height: 30rpx;
		}
		.slash:nth-child(1){
			margin-right: 6rpx;
		}
		.slash:nth-child(2){
			margin-left: 6rpx;
		}
	}
	
</style>
